<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org"
	xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
	<div class="wrapper wrapper-content">
		<header class="main-header">
			<nav class="navbar navbar-static-top1">
				<div class="container">
					<div class="navbar-header">
						<a class="navbar-brand"><b>执行日志</b>
							Console</a>
						<button type="button" class="navbar-toggle collapsed"
							data-toggle="collapse" data-target="#navbar-collapse">
							<i class="fa fa-bars"></i>
						</button>
					</div>
					<div class="navbar-custom-menu">
						<ul class="nav navbar-nav">
							<li><a href="javascript:window.location.reload();"> <i
									class="fa fa-fw fa-refresh"></i>
									刷新
							</a></li>
						</ul>
					</div>

				</div>
			</nav>
		</header>
		<div class="content-wrapper">
			<section class="content">
				<pre style="font-size: 12px; position: relative;">
	                <div id="logConsole"></div>
	                <li class="fa fa-refresh fa-spin"
							style="font-size: 20px; float: left;" id="logConsoleRunning"></li>
	                <div>
							<hr>
							<hr>
						</div>
               </pre>
			</section>
		</div>
	</div>
</body>
<div th:include="include :: footer"></div>
<script>
$(function(){
	// 参数
	var triggerCode = [[${triggerCode}]];
	var handleCode = [[${handleCode}]];
	var executorAddress = `[[${executorAddress}]]`;
	var triggerTime = [[${triggerTime}]];
	var logId = [[${logId}]];
	var prefix = ctx + "job/joblog";
	// trigger fail, end
	if ( !(triggerCode == 200 || handleCode != 0) ) {
	    $('#logConsoleRunning').hide();
	    $('#logConsole').append('<span style="color: red;">任务发起调度失败，无法查看执行日志</span>');
	    return;
	}

	// pull log
	var fromLineNum = 1;    // [from, to], start as 1
	var pullFailCount = 0;
	function pullLog() {
	    // pullFailCount, max=20
	    if (pullFailCount++ > 20) {
	        logRunStop('<span style="color: red;">终止请求Rolling日志,请求失败次数超上限,可刷新页面重新加载日志</span>');
	        return;
	    }

	    // load
	    console.log("pullLog, fromLineNum:" + fromLineNum);

	    $.ajax({
	        type : 'POST',
	        async: false,   // sync, make log ordered
	        url : prefix + '/logDetailCat',
	        data : {
	            "executorAddress":executorAddress,
	            "triggerTime":triggerTime,
	            "logId":logId,
	            "fromLineNum":fromLineNum
	        },
	        dataType : "json",
	        success : function(data){

	            if (data.code == 200) {
	                if (!data.content) {
	                    console.log('pullLog fail');
	                    return;
	                }
	                if (fromLineNum != data.content.fromLineNum) {
	                    console.log('pullLog fromLineNum not match');
	                    return;
	                }
	                if (fromLineNum > data.content.toLineNum ) {
	                    console.log('pullLog already line-end');

	                    // valid end
	                    if (data.content.end) {
	                        logRunStop('<br><span style="color: green;">[Rolling Log Finish]</span>');
	                        return;
	                    }

	                    return;
	                }

	                // append content
	                fromLineNum = data.content.toLineNum + 1;
	                $('#logConsole').append(data.content.logContent);
	                pullFailCount = 0;

	                // scroll to bottom
	                scrollTo(0, document.body.scrollHeight);        // $('#logConsolePre').scrollTop( document.body.scrollHeight + 300 );

	            } else {
	                console.log('pullLog fail:'+data.msg);
	            }
	        }
	    });
	}

	// pull first page
	pullLog();

	// handler already callback, end
	if (handleCode > 0) {
	    logRunStop('<br><span style="color: green;">[Load Log Finish]</span>');
	    return;
	}

	// round until end
	var logRun = setInterval(function () {
	    pullLog()
	}, 3000); 
	function logRunStop(content){
	    $('#logConsoleRunning').hide();
	    logRun = window.clearInterval(logRun);
	    $('#logConsole').append(content);
	}
});


</script>
</html>